// util.js
import Vue from 'vue'

export async function requestApi (u, method, queryString, param) {
  // console.log(Vue.http, method)
  let body = ''

  if (method === 'post') body = param

  let res = await Vue.http[method](`${u}${queryString}`, body)
  if (res.body.status) {
    console.log(u, res.body.data)

    return res.body.data
  } else {
    throw new Error(res.body.msg)
  }
}

export function dateFormat (t, code) {
  let time = new Date(t)
  let [yy, mm, dd, hh, ss, mmm] = [time.getFullYear(), time.getMonth() + 1, time.getDate(), time.getHours(), time.getMinutes(), time.getSeconds()]

  if (mm < 10) mm = `0${mm}`
  if (dd < 10) dd = `0${dd}`
  if (hh < 10) hh = `0${hh}`
  if (ss < 10) ss = `0${ss}`
  if (mmm < 10) mmm = `0${mmm}`

  if (code) return `${yy}-${mm}-${dd}`
  else return `${yy}-${mm}-${dd} ${hh}:${ss}:${mmm}`
}
